package com.itheima.Interceptor;

import com.alibaba.fastjson.JSON;
import com.itheima.common.R;
import com.itheima.common.ThreadF;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MvcInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Long attribute = (Long) request.getSession().getAttribute("qq");
        Long q = (Long) request.getSession().getAttribute("userId");
        // long l = Long.parseLong(q);

        //判断用户是否登录
        if (attribute != null) {

            ThreadF.setCurrentId(attribute);
            return true;
        } else if (q != null) {

            ThreadF.setCurrentId(q);
            return true;
        }
        String notlogin = JSON.toJSONString(R.error("NOTLOGIN"));
        response.getWriter().write(notlogin);
        return false;


    }
}
